<!-- eslint-disable vue/no-useless-template-attributes -->
<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="8">
					<a-form-item label="选择客户" name="customer">
						<a-select v-model:value="searchFormState.customer" placeholder="请选择客户">
							<a-select-option v-for="value in customerOptions" :value="value" :label="value" />
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="选择单据日期" name="orderDate">
						<a-select v-model:value="searchFormState.orderDate" placeholder="请选择选择单据日期">
							<a-select-option v-for="value in dateOptions" :value="value" :label="value" />
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>

		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			// eslint-disable-next-line vue/no-useless-template-attributes
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()">
						<template #icon><plus-outlined /></template>
						流水导入
					</a-button>
					<a-button @click="backFormRef.onOpen(selectedRowKeys)">
						<template #icon><copy-outlined /></template>
						创建回款单
					</a-button>
					<a-button @click="downLoadFile()" :loading="submitDown">
						<template #icon><download-outlined /></template>
						打包下载
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'nccAccountType'">
					{{ $TOOL.dictTypeData('ACCOUNT_CATE', record.nccAccountType) }}
				</template>
				<template v-if="column.dataIndex === 'nccMaterial'">
					{{ $TOOL.dictTypeData('物料分类', record.nccMaterial) }}
				</template>
				<template v-if="column.dataIndex === 'nccAdvancePay'">
					{{ $TOOL.dictTypeData('YSEORNO', record.nccAdvancePay) }}
				</template>
				<template v-if="column.dataIndex === 'payAction'">
					<a-space>
						<a-button @click="createBack()" type="link" v-if="hasPerm('bizBackAdd')">创建NCC回款单</a-button>
					</a-space>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a-popconfirm title="确定要删除吗？" @confirm="deleteBizAiNcc(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('bizAiNccDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
	<backForm ref="backFormRef" @successful="tableRef.refresh()" />
</template>

<script setup name="nccback">
	import { cloneDeep } from 'lodash-es'
	const searchFormState = ref({})
	const customerOptions = ref([])
	const dateOptions = ref([])
	import downloadUtil from '@/utils/downloadUtil'
	import { message } from 'ant-design-vue'
	import Form from './form.vue'
	import backForm from './backForm.vue'
	import bizAiNccApi from '@/api/biz/bizAiNccApi'
	import { load } from '@amap/amap-jsapi-loader'
	const submitDown = ref(false)
	const tableRef = ref()
	const formRef = ref()
	const searchFormRef = ref()
	const backFormRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '单据日期',
			dataIndex: 'executeId',
			align: 'center',
			width: 150
		},
		{
			title: '客户名称',
			dataIndex: 'conNum',
			align: 'center',
			width: 300
		},
		{
			title: 'AI分析结果',
			dataIndex: 'extJson',
			align: 'center'
		},
		{
			title: '回款单创建结果',
			dataIndex: 'nccPayStatus',
			align: 'center'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['bizAiNccEdit', 'bizAiNccDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 100
		})
	}
	// 选项加载
	onMounted(async () => {
		let res = await bizAiNccApi.getOptions()
		customerOptions.value = res.nameList
		dateOptions.value = res.dateList
	})
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}

	const loadData = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		return bizAiNccApi.bizAiNccPage(Object.assign(parameter, searchFormParam)).then((data) => {
			data.records.forEach((i) => {
				if (i.nccConStatus === '1') {
					i.nccConStatus = '成功'
				}
				if (i.nccConStatus === '2') {
					i.nccConStatus = '未成功'
				}
				if (i.nccConStatus === '3') {
					i.nccConStatus = '推送失败'
				}
				if (i.nccPayStatus === '1') {
					i.nccPayStatus = '成功'
				}
				if (i.nccPayStatus === '2') {
					i.nccPayStatus = '未成功'
				}
				if (i.nccPayStatus === '3') {
					i.nccPayStatus = '推送失败'
				}
			})
			return data
		})
	}
	// 重置
	const reset = async () => {
		searchFormRef.value.resetFields()
		let res = await bizAiNccApi.getOptions()
		customerOptions.value = res.nameList
		dateOptions.value = res.dateList
		tableRef.value.refresh(true)
	}
	// 文件打包下载
	const downLoadFile = async () => {
		submitDown.value = true
		console.log(selectedRowKeys.value)
		if (selectedRowKeys.value === undefined) {
			message.info('未选中文件')
		} else {
			let data = {
				nccKey: toRaw(selectedRowKeys.value)
			}
			let res = await bizAiNccApi.downLoadFile(data)
			downloadUtil.resultDownload(res)
		}
		submitDown.value = false
	}

	// 删除
	const deleteBizAiNcc = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizAiNccApi.bizAiNccDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
</script>
